package com.zhuhao.utils.filemoniter;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * 监听加节日自定义配置文件
 *
 * @Author halk
 * @Date 2023/4/25 21:20
 **/
//@Component
@Slf4j
public class FileMonitorListener implements FileAlterationListener {

    /**
     * 自定义节假日配置文件名称
     */
    private static final String HOLIDAY_CONFIG_NAME = "customHoliday.properties";

    public FileMonitorListener() {
        try {
            String holidayPath = "D:\\";
            File file = new File(holidayPath + HOLIDAY_CONFIG_NAME);
            if (!file.exists()) {
                log.error("舆情大屏智能预判，节假日配置文件不存在！位置：config/customHoliday.properties");
            } else {
                reloadFile(file);
            }
            try {
                // 构造观察类主要提供要观察的文件或目录，当然还有详细信息的filter
                FileAlterationObserver observer = new FileAlterationObserver(holidayPath);
                // 为观察对象添加收听对象
                observer.addListener(this);
                // 配置Monitor，第一个参数单位是毫秒，是监听间隔；第二个参数就是绑定我们之前的观察对象。
                FileAlterationMonitor fileMonitor = new FileAlterationMonitor(5000, observer);
                // 启动监听
                fileMonitor.start();
            } catch (Exception e) {
                log.error("fileMonitor start error", e);
            }
        } catch (Exception e) {
            log.error("舆情大屏智能预判，节假日配置文件读取失败：{}", e.getMessage());
        }
    }

    static {

    }

    @Override
    public void onStart(FileAlterationObserver fileAlterationObserver) {
    }

    @Override
    public void onDirectoryCreate(File directory) {
    }

    @Override
    public void onDirectoryChange(File directory) {
    }

    @Override
    public void onDirectoryDelete(File directory) {
    }

    @Override
    public void onFileCreate(File file) {
    }

    @Override
    public void onFileChange(File file) {
        log.info("file change. path:{}", file.getPath());
        if (file.getAbsolutePath().contains(HOLIDAY_CONFIG_NAME)) {
            log.info("自定义日期配置文件改变！path:{}", file.getPath());
            reloadFile(file);
        }
    }

    @Override
    public void onFileDelete(File file) {
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
    }

    /**
     * 处理改变的文件
     *
     * @param file
     */
    private static void reloadFile(File file) {
        Properties properties = new Properties();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            //使用properties对象加载输入流
            properties.load(bufferedReader);
            //根据key获取对应的value
            String value = properties.getProperty("customHoliday");
//            this.customHolidayProperties.coverList(value);
            log.info("节假日自定义配置文件重新加载! customHoliday: {}", value);
            bufferedReader.close();
        } catch (IOException e) {
            log.error("节假日自定义配置文件重新加载失败！", e);
        }
    }

}
